<?php
    class DatatypesController
    {
        private $model;
        private $view;
        
        public function __construct($model, $view) {
            $this->model = $model;
            $this->view  = $view;
            $view->setController($this);
        }
        
        // Parse the actions
        public function parseActions() {
            // See if there are actions to be taken:
            if(isset($_POST['save'])) {
                // Save the datatype:
                $name        = makeSafe($_POST['name']);
                $description = makeSafe($_POST['description']);
                $action      = makeSafe($_POST['action']);
                $values      = array('name'=>$name, 'description'=>$description);
                if($action=='add') {
                    $id = $this->model->addDatatype($values);
                } elseif($action=='edit') {
                    $id = $_POST['id'];
                    $this->model->updateDatatype($id, $values);                    
                }
                // Store the options:
                $options = array();
                foreach($_POST as $key=>$value) {
                    if(substr($key, 0, 7)=='option_') {
                        // This is an option!
                        $a  = explode('_', $key);
                        $nr = $a[1];
                        $hasmany = isset($_POST['many_'.$nr]) ? 1 : 0;
                        array_push($options, array('id'=>$value, 'hasmany'=>$hasmany));
                    }
                }                
                $this->model->linkOptions($id, $options);
            } elseif(isset($_POST['delete'])) {
                // Delete the datatype:
                $id = $_POST['id'];
                $this->model->deleteDatatype($id);
            }
        }
        
        // Render the interface
        public function render() {
            // Show the interface:
            $this->view->showTop();
            if(isset($_POST['add'])) {
                $values = array('name'=>'', 'description'=>'', 'id'=>'', 'action'=>'add');
                $this->view->showForm($values);
            } elseif (isset($_POST['edit'])) {                
                $id               = $_POST['id'];
                $values           = $this->model->getDatatype($id);
                $values['action'] = 'edit';
                $this->view->showForm($values);
            } else {
                $records = $this->model->getRecords();
                if(count($records)==0) {
                    $this->view->showNoRecordsAvailable();    
                } else {
                    $this->view->showRecords($records);
                }
                $this->view->showAddEditDelete();
            }
            $this->view->showBottom();
        }
        
        // Get a list of options of this datatype
        public function getOptions($id) {
            return $this->model->getOptions($id);
        }
    }
?>